import 'package:flutter/material.dart';
import 'package:flutter_base/common/dateFormatter.dart';
import 'package:flutter_base/common/dialog.dart';
import 'package:flutter_base/common/routeUtil.dart';
import 'package:flutter_base/page/pageStateless.dart';
import 'package:flutter_base/widgets/itemWidgets.dart';
import 'package:flutter_base/widgets/refreshListView.dart';
import 'package:flutter_general/demo/scannerPage.dart';
import 'package:flutter_general/demo/takePicPage.dart';

import 'indexPageLogic.dart';

/// Create： Zhang 2021/6/28 10:03 上午
/// erp:     ext.zhangjunfei
/// 描述：索引页

class IndexPAge extends PageStateless<IndexPageLogic> {
  IndexPAge() : super(title: "使用示例", logic: IndexPageLogic());

  @override
  Widget? leading(BuildContext context) {
    return Container();
  }

  @override
  Widget? content(BuildContext context) {
    return refreshListView(
      dividerColor: Colors.transparent,
      itemBuilder: (context, index) {
        return itemWidget(
          context,
          text: pageLogic.indexItems[index],
          onClick: () {
            switch (pageLogic.indexItems[index]) {
              case "弹框":
                showDialog(context);
                break;
              case "日期格式化":
                showAlert(
                  context,
                  msg: DateFormatter.formatDateTime(DateTime.now()),
                  actions: [AlertDialogAction()],
                );
                break;
              case "时间选择":
                pickTime(context);
                break;
              case "照片选择":
                goTo(context, TakePicPage());
                break;
              case "扫描二维码":
                goTo(
                  context,
                  ScannerPage(),
                  onPageResult: (res) {
                    if (res != null) {
                      toast(res.toString());
                    }
                  },
                );
                break;
            }
          },
        );
      },
      itemCount: pageLogic.indexItems.length,
      refresh: () {
        toast("刷新数据");
      },
      isNeedLoadingMore: false,
    );
  }

//-------------方法定义----------------

  ///展示弹框
  showDialog(BuildContext context) {
    showBottomMenu(context,
        items: ["提示", "失败", "成功", "toast", "loading", "对话框"],
        callback: (index, item) {
      switch (item) {
        case "提示":
          toastInfo("提示消息");
          break;
        case "失败":
          toastError("错误消息");
          break;
        case "成功":
          toastSuc("成功消息");
          break;
        case "toast":
          toast("提示消息");
          break;
        case "loading":
          toastLoading();
          Future.delayed(Duration(seconds: 2))
              .then((value) => dismissLoading());
          break;
        case "对话框":
          showAlert(
            context,
            msg: "提示消息",
            actions: [AlertDialogAction()],
          );
          break;
      }
    });
  }

  ///时间选择
  void pickTime(BuildContext context) {
    showDateTimePicker(context, onConfirm: (time) {
      toast(time.millisecondsSinceEpoch.toString());
    });
  }
//-------------方法定义----------------
}
